package com.supor.aiot.common.ui.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.AndroidViewModel;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.baseconfig.common.utils.SystemUtils;
import com.android.baseconfig.constact.AppGlobalHost;
import com.supor.aiot.common.R;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceActivity;
import com.supor.aiot.common.databinding.ActivityCommunityWebBinding;
import com.supor.aiot.common.ui.view.webkit.WebKitHelper;
import com.supor.aiot.common.utils.FullscreenInputWorkaround;
import com.supor.aiot.common.utils.MPLoggerUtils;

@Route(path = PathUrl.PATH_MINE_USER_CHANNEL)
public class CommunityWebActivity extends BaseMVVMEnhanceActivity<AndroidViewModel, ActivityCommunityWebBinding> {

    private WebKitHelper webKitHelper;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        FullscreenInputWorkaround.assistActivity(this, bindingView.getRoot(), new FullscreenInputWorkaround.InputShowListener() {
            @Override
            public void inputShow(boolean show) {

            }
        });

//        int statusBarHeight = SystemUtils.getDefaultStatusHeight(this);
//        bindingView.flContainer.setPadding(bindingView.flContainer.getPaddingLeft(),
//                statusBarHeight,
//                bindingView.flContainer.getPaddingRight(),
//                bindingView.flContainer.getPaddingBottom());
        if (null != getIntent()) {
            userId = getIntent().getStringExtra("USER_ID");
        }
        webKitHelper = new WebKitHelper();
        webKitHelper.embedWebKit(this, bindingView.flContainer);
        webKitHelper.loadUrl(AppGlobalHost.getCommHost() + "/pages/communityMy/index?id=" + userId + "&isUser=true&globalHeight="
                + SystemUtils.getDefaultStatusHeight(this));
        MPLoggerUtils.userChannelCount();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != webKitHelper) {
            webKitHelper.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webKitHelper.canGoBack()) {
            webKitHelper.goBack();//返回上个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);//退出H5界面
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != webKitHelper) {
            webKitHelper.sendPageStateToWeb(0);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != webKitHelper) {
            webKitHelper.sendPageStateToWeb(1);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != webKitHelper) {
            webKitHelper.destoryKit();
        }
    }
}